package com.base.self.controller.web;

import com.base.exception.BaseException;
import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.dto.ptTeam.PtIntegralDto;
import com.base.self.dto.ptTeam.PtTeamWebListDto;
import com.base.self.entity.Ptintegral;
import com.base.self.service.IPtintegralService;
import com.base.service.SubscribersService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

@RestController
@Slf4j
@RequestMapping("/admin")
public class PtIntegralWebController {

    private final IPtintegralService iPtintegralService;
    private final SubscribersService subscribersService;

    public PtIntegralWebController(IPtintegralService iPtintegralService, SubscribersService subscribersService) {
        this.iPtintegralService = iPtintegralService;
        this.subscribersService = subscribersService;
    }

    @GetMapping(value = "/ptIntegral")
    public ModelAndView moneyGoldIndex() {
        return new ModelAndView("ptTeam/ptIntegral");
    }

    @PostMapping(value = "/ptIntegralWebList")
    @Validated
    public ResponseResult moneyGoldWebList(@Valid final PtTeamWebListDto ptIntegralDto) {
        return iPtintegralService.selectWebList(ptIntegralDto);
    }

    @PostMapping(value = "/ptIntegralWebAdd")
    public ResponseResult ptIntegralWebAdd(@ApiParam("用户Id") @RequestParam String addMobile, @ApiParam("addStatus") @RequestParam Integer addStatus, @ApiParam("addType") @RequestParam Integer addType, @ApiParam("addMoney") @RequestParam BigDecimal addMoney, @ApiParam("addContext") @RequestParam String addContext) {
        Subscribers sub = Optional.ofNullable(subscribersService.selectBySubscribers(addMobile, null)).orElseThrow(() -> new BaseException(ResponseResultUtils.getResponseResultF("新增用户为空")));
        if (StringUtils.isEmpty(addContext)) {
            addContext = "后台管理新增记录";
        }
        Ptintegral ptintegral = new Ptintegral(null, sub.getId(), new Date(), addMoney, addStatus, addType, addContext);
        iPtintegralService.getBaseMapper().insert(ptintegral);
        return ResponseResultUtils.getResponseResultS("新增成功");
    }

    @PostMapping(value = "/ptIntegralWebUpdate")
    @Validated
    public ResponseResult moneyGoldWebUpdate(@Valid final PtIntegralDto ptIntegralDto) {
        Ptintegral ptintegral = Optional.ofNullable(iPtintegralService.getBaseMapper().selectById(ptIntegralDto.getUpdateId())).orElseThrow(() -> new BaseException(ResponseResultUtils.getResponseResultF("数据异常")));
        String userId = null;
        if (!StringUtils.isEmpty(ptIntegralDto.getUpdateMobile())) {
            userId = Optional.ofNullable(subscribersService.selectBySubscribers(ptIntegralDto.getUpdateMobile(), null)).orElseThrow(() -> new BaseException(ResponseResultUtils.getResponseResultF("获得积分的用户不存在"))).getId();
        }
        ptintegral.setMoney(ptIntegralDto.getUpdateMoney());
        ptintegral.setStatus(ptIntegralDto.getUpdateStatus());
        ptintegral.setSub(userId);
        ptintegral.setType(ptIntegralDto.getUpdatetype());
        iPtintegralService.getBaseMapper().updateById(ptintegral);
        return ResponseResultUtils.getResponseResultS("更新记录成功");
    }


    @PostMapping(value = "/ptIntegralWebDelete")
    public ResponseResult moneyGoldWebDelete(@ApiParam("id") @RequestParam Integer id) {
        iPtintegralService.getBaseMapper().deleteById(id);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }
}
